﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using ProjectBase.Core;
using ProjectBase.Core.Web;

using Survey.Common;

namespace Survey.Web.Controls
{
    public abstract class ButtonsBlock<TModel> : RazorControl<TModel>
        where TModel : class, ICoreModel
    {
        /// <summary>
        ///     Renders the buttons TextLocalized property if not empty
        ///     or calls GetLocalized(button.Text)
        /// </summary>
        /// <param name = "button"></param>
        /// <returns></returns>
        protected virtual string GetText(IButtonItem button)
        {
            if (button.TextLocalized.IsNotEmpty())
            {
                return button.TextLocalized;
            }
            return button.Text.IsNotEmpty()
                    ? GetLocalized(button.Text)
                    : GetLocalized(button.ButtonName);
        }
        /// <summary>
        ///     For Delete button appends confirmation dialogue
        /// </summary>
        /// <returns></returns>
        protected virtual string AssureDelete(IButtonItem button)
        {
            var model = Model as IEntityModel<IPersistentObject, IFilter>;
            if (model.IsNull())
            {
                return null;
            }
            if (model.Item.Is()
              && button.FormAction.IsEqual(Str.Actions.NoView.Delete))
            {
                return GetLocalized(Str.WebControls.Common.DoDelete2)
                    .FormatWith(model.Item.IDToDisplay, model.Item.ToDisplay());
            }
            if (model.ListModel.Is()
              && button.FormAction.IsEqual(Str.Actions.NoView.DeleteAll))
            {
                return GetLocalized(Str.WebControls.Common.DoDeleteAll1)
                    .FormatWith(model.ListModel.ItemsToDisplay.CountOrDefault());
            }
            return null;
        }

        /// <summary>
        ///     Sets the accesKey for some special actions,
        ///     renders the 'title' tag if any title information exists
        /// </summary>
        /// <param name = "button"></param>
        /// <returns></returns>
        protected virtual string GetTitle(IButtonItem button)
        {
            var title = button.Title ?? GetText(button);

            switch (button.FormAction)
            {
                case Str.Actions.Edit:
                    return title + " (Shift + Alt + E)";
                case Str.Actions.NoView.CancelDefault:
                    return title + " (Shift + Alt + C)";
                case Str.Actions.NoView.CancelDetail:
                    return title + " (Shift + Alt + C)";
                case Str.Actions.NoView.CancelEdit:
                    return title + " (Shift + Alt + C)";
                case Str.Actions.NoView.Clear:
                    return title + " (Shift + Alt + C)";
                case Str.Actions.Special.SaveAndClose:
                    return title + " (Shift + Alt + A)";
            }
            return title;
        }
        protected virtual string GetAccessKey(IButtonItem button)
        {
            switch (button.FormAction)
            {
                case Str.Actions.Edit:
                    return "e";
                case Str.Actions.NoView.CancelDefault:
                    return "c";
                case Str.Actions.NoView.CancelDetail:
                    return "c";
                case Str.Actions.NoView.CancelEdit:
                    return "c";
                case Str.Actions.NoView.Clear:
                    return "c";
                case Str.Actions.Special.SaveAndClose:
                    return "a";
            }
            return null;
        }
    }
}